package com.bdcom.dialog;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.comp.CanvasPanel;
import com.bdcom.util.PlannerUtil;

@Component
public class WidthHeightValueDialog extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField widthField;

	private JTextField heightField;

	private JLabel widthLabel;

	private JLabel heightLabel;

	private JButton saveButton;

	@Autowired
	private CanvasPanel canvasPanel;
	
	public WidthHeightValueDialog() {

		JPanel panel = new JPanel();

		GridBagLayout layout = new GridBagLayout();

		GridBagConstraints constrains = new GridBagConstraints();

		panel.setLayout(layout);

		widthField = new JTextField(10);

		heightField = new JTextField(10);

		widthLabel = new JLabel(PlannerUtil.getString("width"));

		heightLabel = new JLabel(PlannerUtil.getString("height"));

		saveButton = new JButton(PlannerUtil.getString("save"));

		saveButton.addActionListener(this);

		widthField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent evt) {
				int keyChar = evt.getKeyChar();
				if ((keyChar >= 48 && keyChar <= 57) || (keyChar == 46) || keyChar == 8 || keyChar == 127) {

				} else {
					evt.consume(); // 关键，屏蔽掉非法输入
				}
			}

			public void keyReleased(KeyEvent e) {

				int imageWidth = 0;

				int imageHeight = 0;

				try {
					imageWidth = canvasPanel.getCanvasModel().getBackImg().getWidth();

					imageHeight = canvasPanel.getCanvasModel().getBackImg().getHeight();
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				if (imageWidth == 0 || imageHeight == 0) {
					JOptionPane.showMessageDialog(null, PlannerUtil.getString("Please set image's Width and Height"));
					return;
				}

				String widthString = widthField.getText();

				if (widthString == null || "".equals(widthString)) {
					return;
				}

				Double width = 0D;

				try {
					width = Double.parseDouble(widthString);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null, PlannerUtil.getString("Please input a true number value"));
					return;
				}

				Double height = width * imageHeight / imageWidth;

				heightField.setText(PlannerUtil.format.format(height));

			}

		});

		heightField.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent evt) {

				int keyChar = evt.getKeyChar();
				if ((keyChar >= 48 && keyChar <= 57) || (keyChar == 46) || keyChar == 8 || keyChar == 127) {

				} else {
					evt.consume(); // 关键，屏蔽掉非法输入
				}
			}

			public void keyReleased(KeyEvent e) {
				int imageWidth = 0;

				int imageHeight = 0;

				try {
					imageWidth = canvasPanel.getCanvasModel().getBackImg().getWidth();

					imageHeight = canvasPanel.getCanvasModel().getBackImg().getHeight();
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				if (imageWidth == 0 || imageHeight == 0) {
					JOptionPane.showMessageDialog(null, PlannerUtil.getString("Please set image's Width and Height"));
					return;
				}

				String heightString = heightField.getText();

				if (heightString == null || "".equals(heightString)) {
					return;
				}

				Double height = 0D;

				try {
					height = Double.parseDouble(heightString);
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null, PlannerUtil.getString("Please input a true number value"));
					return;
				}

				Double width = height * imageWidth / imageHeight;

				widthField.setText(PlannerUtil.format.format(width));

			}

		});

		constrains.insets = new Insets(5, 0, 0, 10);

		constrains.gridx = 0;

		panel.add(widthLabel, constrains);

		constrains.gridx = 1;
		
		panel.add(widthField, constrains);
		
		constrains.gridx = 0;
		
		panel.add(heightLabel, constrains);
		
		constrains.gridx = 1;
		
		panel.add(heightField,constrains);

		constrains.gridx = 0;

		constrains.gridwidth = 2;

		constrains.insets = new Insets(20, 0, 0, 10);

		panel.add(saveButton, constrains);

		this.getContentPane().add(panel);

		this.setTitle(PlannerUtil.getString("Width and Height"));
		this.setSize(300, 200);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setModal(true);
		this.setResizable(false);
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);

		widthField.setText(new DecimalFormat("#0.0").format(canvasPanel.getCanvasModel().getMapWidth()));

		heightField.setText(new DecimalFormat("#0.0").format(canvasPanel.getCanvasModel().getMapHeight()));

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		double w;

		double h;

		try {

			w = Double.parseDouble(widthField.getText());

			h = Double.parseDouble(heightField.getText());

		} catch (Exception e2) {

			JOptionPane.showMessageDialog(null, PlannerUtil.getString("Please input a true number value"));
			return;
		}
		canvasPanel.getCanvasModel().setMapWidth(w);

		canvasPanel.getCanvasModel().setMapHeight(h);

		this.setVisible(false);

		canvasPanel.resizeScroll();

	}

}
